//
//  SHTabBar.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/19.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

class SHTabBar: UITabBar {
    
    //声明闭包
    var closure:(()->())?
    
    //MARK: - 懒加载控件
    private lazy var composeButton: UIButton = UIButton(
        backgroundImgName: "tabbar_compose_button",
        imgName: "tabbar_compose_icon_add",
        isHighlighted: true,
        isSizeToFit: true, 
        target: self,
        action: "didClickBtn")
    
    //MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听方法
    func didClickBtn(){
        closure?()
    }
    
    //MARK: - 设置界面
    private func setupUI() {
        addSubview(composeButton)
    }
    
    //MARK: - 子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let expectedWidth = frame.width * 0.2
        var index: CGFloat = 0
        //遍历子控件
        for value in subviews {
            // 判断value 是否是UITabBarButton Class
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //设置frame
                value.frame.origin.x = expectedWidth * index
                value.frame.size.width = expectedWidth
                
                index++
                if index == 2 {
                    index++
                }
            }
        }
        //布局composeButton
        composeButton.center.x = frame.width * 0.5
        composeButton.center.y = frame.height * 0.5
    }
    
}
